package com.hxkcloud.nurse.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Message;

import com.hxkcloud.nurse.beans.LoginInfo;


/**
 * Created by ray_lei on 2016/7/5.
 */
public class StorageManager {


    private static Context mContext;

    private static StorageManager mStorageManager = null;
    // 数据库对象
    private static SQLiteDatabase db = null;

    private static YjhDBManager manager = null;

    /**
     * 无参构造方法
     */
    private StorageManager() {
        super();
    }

    /**
     * 创建单实例对象
     *
     * @param context
     * @return
     */
    public static StorageManager getInstance(Context context) {
        if (mStorageManager == null) {
            mStorageManager = new StorageManager();
        }
        initDB(context);
        mContext = context;
        return mStorageManager;
    }

    /**
     * 初始化数据库,每次数据库操作前都需要调用该方法，打开数据库
     *
     * @param context
     */
    private static void initDB(Context context) {
        if (manager == null) {
            manager = new YjhDBManager(context);
        }
        db = manager.getDatabase();
    }

    /**
     * @Description: 关闭数据库
     * @return: void
     */
    private void closeDB() {
        try {
            if (manager != null)
                manager.closeDatabase();
            if (db != null)
                db.close();
            manager = null;
            db = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 数据库查询结果信息
     *
     * @param what
     * @param obj
     * @return
     */
    public static Message createMsg(int what, Object obj) {
        Message msg = new Message();
        msg.what = what;
        if (obj != null)
            msg.obj = obj;
        return msg;
    }


    /**
     * 具体的数据库操作
     */


    /*
     * 用户登录数据保存
     */
    public long insertUserLoginInfo(LoginInfo info) {
        initDB(mContext);

        deleteUserLoginInfo();

        ContentValues values = new ContentValues();
        values.put("TOKEN", info.getToken());
        values.put("USERID", info.getUserId());
        values.put("USERSTYLE", info.getUserStyle());
        values.put("NAME", info.getName());
        values.put("AGE", info.getAge());
        values.put("SEX", info.getSex());
        values.put("HEIGHT", info.getHeight());
        values.put("WEIGHT", info.getWeight());
        values.put("HEADIMAGE", info.getHeadImg());
        values.put("ADDRESS", info.getAddress());
        values.put("DEPARTMENTID", info.getDepartmentId());
        values.put("TITLE", info.getTitle());
        values.put("WORKINGYEARS", info.getWorkingYears());
        values.put("SCHOOL", info.getSchool());
        values.put("WORKUNIT", info.getWorkUnit());
        values.put("SPECIALTY", info.getSpecialty());
        long insert = db.insert("yjh_userlogin", null, values);
        closeDB();
        return insert;
    }

    /*
     * 获取用户登录信息
     */
    public LoginInfo getUserLoginInfo() {
        initDB(mContext);
        LoginInfo info = null;
        try {

            String sql = "SELECT * FROM YJH_USERLOGIN";
            Cursor cursor = db.rawQuery(sql, null);
            if (cursor.moveToFirst()) {
                info = new LoginInfo();
                info.setToken(cursor.getString(cursor.getColumnIndex("TOKEN")));
                info.setUserId(cursor.getString(cursor.getColumnIndex("USERID")));
                info.setUserStyle(cursor.getString(cursor
                        .getColumnIndex("USERSTYLE")));
                info.setName(cursor.getString(cursor
                        .getColumnIndex("NAME")));
                info.setAge(cursor.getString(cursor
                        .getColumnIndex("AGE")));
                info.setSex(cursor.getString(cursor.getColumnIndex("SEX")));
                info.setHeight(cursor.getString(cursor
                        .getColumnIndex("HEIGHT")));
                info.setWeight(cursor.getString(cursor
                        .getColumnIndex("WEIGHT")));
                info.setHeadImg(cursor.getString(cursor
                        .getColumnIndex("HEADIMAGE")));
                info.setAddress(cursor.getString(cursor
                        .getColumnIndex("ADDRESS")));
                info.setDepartmentId(cursor.getString(cursor
                        .getColumnIndex("DEPARTMENTID")));
                info.setTitle(cursor.getString(cursor
                        .getColumnIndex("TITLE")));
                info.setWorkingYears(cursor.getString(cursor
                        .getColumnIndex("WORKINGYEARS")));
                info.setSchool(cursor.getString(cursor
                        .getColumnIndex("SCHOOL")));
                info.setWorkUnit(cursor.getString(cursor
                        .getColumnIndex("WORKUNIT")));
                info.setSpecialty(cursor.getString(cursor
                        .getColumnIndex("SPECIALTY")));
            }
        } catch (Exception e) {
            return null;
        } finally {
            closeDB();
        }
        return info;
    }

    public int updateUserLoginInfo(LoginInfo info) {
        initDB(mContext);
        ContentValues values = new ContentValues();
        values.put("TOKEN", info.getToken());
        values.put("USERID", info.getUserId());
        values.put("USERSTYLE", info.getUserStyle());
        values.put("NAME", info.getName());
        values.put("AGE", info.getAge());
        values.put("SEX", info.getSex());
        values.put("HEIGHT", info.getHeight());
        values.put("WEIGHT", info.getWeight());
        values.put("HEADIMAGE", info.getHeadImg());
        values.put("ADDRESS", info.getAddress());
        values.put("DEPARTMENTID", info.getDepartmentId());
        values.put("TITLE", info.getTitle());
        values.put("WORKINGYEARS", info.getWorkingYears());
        values.put("SCHOOL", info.getSchool());
        values.put("WORKUNIT", info.getWorkUnit());
        values.put("SPECIALTY", info.getSpecialty());
        int update = db.update("yjh_userlogin", values, " USERID=? ",
                new String[]{info.getUserId()});
        closeDB();
        return update;
    }

    /*
     * 删除用户数据，在退出登录时调用
     */
    public int deleteUserLoginInfo() {
        initDB(mContext);
        int delete = -1;
        delete = db.delete("yjh_userlogin", null, null);
        closeDB();
        return delete;
    }


}
